using System;
using System.Windows.Forms;
using NHibernate.Exceptions;

namespace ProyectoSelva.Validate
{
    public class ActionExecutor
    {
        public  static void Execute(IValidateForm form, CallbackAction callbackAction)
        {
            if (form.IsValid())
            {
                try
                {
                    callbackAction();
                }
                catch(GenericADOException e)
                {
                    MessageBox.Show(e.ToString().Contains(@"System.Data.SqlClient.SqlException: Violation of UNIQUE KEY")
                                        ? @"Actualmente existe un registro con el mismo codigo"
                                        : e.ToString());
                }
                catch(Exception e)
                {
                    //TODO Agregar mas logica de error
                    MessageBox.Show(e.ToString());
                }
            }
        }

        public delegate void CallbackAction();
    }
}